package com.zhanglifan.action;

import com.alibaba.fastjson.JSON;
import com.zhanglifan.service.SqlService;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Information
 * Author: ZhangLifan
 * Time: 2017/10/10 19:58
 * Description: statChartAction
 */
public class StatChartAction extends BaseAction {
    private SqlService sqlService;

    public void setSqlService(SqlService sqlService) {
        this.sqlService = sqlService;
    }

    public String factorysale(){
        return "factorysaleSuccess";
    }

    public String onlineinfo(){
        return "onlineinfoSuccess";
    }

    public String productsale() {
        return "productsaleSuccess";
    }

    public void ajaxProductsale(){
        List list = sqlService.executeSQL("SELECT PRODUCT_NO,SUM(NVL(CNUMBER,0)) SUM FROM CONTRACT_PRODUCT_C GROUP BY PRODUCT_NO ORDER BY SUM DESC");

        Map map = new HashMap();
        List pList = new ArrayList();
        List sumList = new ArrayList();
        map.put("productNo",pList);
        map.put("value",sumList);

        for (int i = 0; i < list.size(); i++){
            pList.add(list.get(i));
            sumList.add(list.get(++i));
        }

        String jsonStr = JSON.toJSONString(map);

        try {
            ServletActionContext.getResponse().getWriter().write(jsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void ajaxOnlineinfo(){
        List list = sqlService.executeSQL("SELECT TO_CHAR(LOGIN_TIME,'HH24') TIME,COUNT(1) C FROM LOGIN_LOG_P GROUP BY TO_CHAR(LOGIN_TIME,'HH24') ORDER BY TIME");


        Map map = new HashMap();
        List timeList = new ArrayList();
        List countList = new ArrayList();

        for (int i = 0; i<24; i++) {
            String timeStr = i < 10 ? "0"+String.valueOf(i) : String.valueOf(i);
            timeList.add(timeStr);
            if(list.contains(timeStr)){
                countList.add(Integer.parseInt((String)list.get(list.indexOf(timeStr)+1)));
            }else{
                countList.add(0);
            }
        }

        map.put("time",timeList);
        map.put("count",countList);

        String jsonStr = JSON.toJSONString(map);

        try {
            ServletActionContext.getResponse().getWriter().write(jsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void ajaxFactorysale(){
        try {
            List list = sqlService.executeSQL("select FACTORY_NAME,SUM(CNUMBER) SUM from CONTRACT_PRODUCT_C GROUP BY FACTORY_NAME ORDER BY SUM DESC");

            List mapList = new ArrayList();

            for (int i = 0; i < list.size() ; i++) {
                Map map = new HashMap<>();
                map.put("factory",list.get(i));
                map.put("value",list.get(++i));
                mapList.add(map);
            }

            String jsonStr  = JSON.toJSONString(mapList);

            HttpServletResponse response = ServletActionContext.getResponse();
            response.setContentType("application/json; charset=utf-8");
            //此处必须用标准json格式,即双引号(不能用单引号),否则无法调用回调函数
            //response.getWriter().write("{\"abc\":\"123\"}");
            response.getWriter().write(jsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
